﻿using Component;
using Painter.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;

namespace Painter.Component
{
    public class PaintCanvas : InkCanvas
    {
        public Adorners mAdorner;
        BitmapCanvas mBitmapCanvas = new BitmapCanvas();
        public PaintCanvas()
        {
            Children.Add(mBitmapCanvas);

            UseCustomCursor = true;
            VerticalAlignment = VerticalAlignment.Top;
            HorizontalAlignment = HorizontalAlignment.Left;
            Background = System.Windows.Media.Brushes.Transparent;
            DefaultDrawingAttributes.StylusTip = StylusTip.Ellipse;
            VisualBitmapScalingMode = BitmapScalingMode.NearestNeighbor;
            RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor);
        }
        public void DrawPicture(DrawingVisual image)
        {
            mBitmapCanvas.Add(image);
        }
        public void DrawFloatPicture(DrawingVisual drawing)
        {
            OnEditingModeChanged(null);
            AAdorner adorner = AdornerControl.CreateAdorner(this, Adorners.Rect);
            adorner.EndUpdate(drawing);

            List<FrameworkElement> collection = (Parent as Grid)?.Children.Cast<FrameworkElement>().ToList();
            if (drawing.DescendantBounds.Width > Width)
            {
                (Parent as Grid).Width = drawing.DescendantBounds.Width;
                collection?.ForEach(o => o.Width = drawing.DescendantBounds.Width);
            }
            if (drawing.DescendantBounds.Height > Height)
            {
                (Parent as Grid).Height = drawing.DescendantBounds.Height;
                collection?.ForEach(o => o.Height = drawing.DescendantBounds.Height);
            }
            ScaleData scale_data = ScaleData.Find_CreateScaleData(this);
            scale_data.BaseWidth = Width;
            scale_data.BaseHeight = Height;
            scale_data.UpdateWidthHeight();
        }
        protected override void OnEditingModeChanged(RoutedEventArgs e)
        {
            AAdorner adorner = AdornerControl.GetAdorner(this);
            if (adorner!=null)
            {
                DrawingVisual drawing = adorner.GetImage(this);
                if (drawing?.Drawing != null) DrawPicture(drawing);
                AdornerControl.Clear(this);
            }
        }
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            Console.Out.WriteLine(VisualScrollableAreaClip);
            if (e.Key == Key.Escape)
                OnEditingModeChanged(null);
        }
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            CaptureMouse();
            OnEditingModeChanged(null);
            if (EditingMode == InkCanvasEditingMode.Select)
                AdornerControl.CreateAdorner(this, mAdorner);

            if (PaintSettings.inst().mOilCursor == Cursor)
                Background = new SolidColorBrush(DefaultDrawingAttributes.Color);
        }
        protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            ReleaseMouseCapture();
            AAdorner adorner = AdornerControl.GetAdorner(this);
            if (adorner !=null && EditingMode == InkCanvasEditingMode.Select)
            {//截图
                adorner.EndUpdate(mBitmapCanvas.GetImage(adorner.mGeometry));
            }
        }
        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            if (EditingMode == InkCanvasEditingMode.Select&& e.LeftButton == MouseButtonState.Pressed)
            {
                AAdorner adorner = AdornerControl.GetAdorner(this);
                System.Windows.Point pt = e.GetPosition(this);
                adorner?.BeginUpdate(pt);
            }
        }
    }
}
